package com.xjh.o2o.until;

 

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

 




import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;


/**
 * @description 
 * @author xiejh
 * @date 2018年3月14日 下午3:15:48 
 * @version 
 */

public class ImageUtil{
	private static String basePath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
	/**
	 * 生成时间格式
	 */
	private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	/**
	 * 生成随机数
	 */
	private static final Random r = new Random();
	
	
	private static Logger logger=LoggerFactory.getLogger(ImageUtil.class);
	
	
	/**
	 * 
	 * @description 将CommonsMultipartFile转化为File
	 * @param cFile
	 * @return
	 * @author xiejh
	 * @date 2018年3月16日 下午2:16:15
	 */
	public static File transferCommonsMultipartFileToFile (CommonsMultipartFile cFile){
		File newFile=new File(cFile.getOriginalFilename());
		try {
			cFile.transferTo(newFile);
		} catch (IllegalStateException e) {
			logger.error(e.toString());
			e.printStackTrace();
		} catch (IOException e) {
			logger.error(e.toString());
			e.printStackTrace();
		}
		return newFile;
		
	}
	/**
	 * 处理缩略图，并返回新生成图片的相对值路径
	 * 
	 * @param thumbnail
	 * @param targetAddr
	 * @return
	 */
	public static String generateThumbnail(InputStream thumbnailInputStream,String fileName, String targetAddr) {
		String realFileName=getRandomFileName();
		String extension=getFileExtension(fileName);
		makeDirPath(targetAddr);
		
		String relativeAddr=targetAddr+realFileName+extension;
		
		logger.debug("current relativeAdder is "+relativeAddr);
		File dest=new File(PathUtil.getImagBasePath()+relativeAddr);
		logger.debug("current complete addr is "+PathUtil.getImagBasePath()+relativeAddr);

		try {
			Thumbnails.of(thumbnailInputStream).size(200, 200)
			.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
			.outputQuality(0.8f).toFile(dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return relativeAddr;
		
	}
	
	/**
	 * 
	 * @description  创建目标路径所涉及到的目录，即/home/work/xiangze/xxx.jpg, 那么 home work xiangze  这三个文件夹都得自动创建
	 * @param targetAddr
	 * @author xiejh
	 * @date 2018年3月16日 上午11:08:49
	 */
	 
	private static void makeDirPath(String targetAddr) {
		String realfileParentPath= PathUtil.getImagBasePath()+targetAddr;
		File dirPath = new File(realfileParentPath);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
	}


	/**
	 * 
	 * @description 获取输入文件流的扩展名
	 * @param thumbnail
	 * @return
	 * @author xiejh
	 * @date 2018年3月16日 上午10:59:04
	 */
	private static String getFileExtension(String fileName) {
		 
		return fileName.substring(fileName.lastIndexOf("."));
	}
 
	
	/**
	 * 生成随机文件名，当前年月日小时分钟秒钟+五位随机数
	 * 
	 * @return
	 */
	public static String getRandomFileName() {
		// 获取随机的五位数
		int rannum = r.nextInt(89999) + 10000;
		String nowTimeStr = sDateFormat.format(new Date());
		return nowTimeStr + rannum;
	}
	
	

	public static void main(String[] args) throws IOException {  
		Thumbnails.of(new File("C:/Users/Administrator/Desktop/fff.jpg"))
		.size(200, 200).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
		.outputQuality(0.8f).toFile("C:/Users/Administrator/Desktop/fffnew.jpg");
	}
	
}